//
//  ViewController.m
//  GCDdemo
//
//  Created by XiaoMing on 16/3/19.
//  Copyright © 2016年 XiaoMing. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    [self methods1];
//    [self methods2];
//    [self methods3];
    [self methods4];
//    [self methods5];
}

//1、常用的方法dispatch_async

-(void)methods1{
    NSLog(@"初次进入－－－创建分线程之前");
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [NSThread sleepForTimeInterval:2];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"分线程－－－调用主线程");
        });
    });
    
    NSLog(@"初次进入－－－创建分线程之后");
}

//2、dispatch_group_async可以实现监听一组任务是否完成，完成后得到通知执行其他的操作。

-(void)methods2{

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_async(group, queue, ^{
         [NSThread sleepForTimeInterval:4];
          NSLog(@"分线程group1");
    });
    dispatch_group_async(group, queue, ^{
        [NSThread sleepForTimeInterval:3];
        NSLog(@"分线程group2");
    });
    dispatch_group_async(group, queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"分线程group3");
    });
    dispatch_group_async(group, queue, ^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@"分线程group4");
    });
    dispatch_group_notify(group, queue, ^{
        NSLog(@"update OVER");
    });
}

//3、dispatch_barrier_async是在前面的任务执行结束后它才执行

-(void)methods3{
    dispatch_queue_t queue = dispatch_queue_create("gcdtes", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:4];
        NSLog(@"dispatch_async1");
    });
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"dispatch_async2");
    });
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@"dispatch_async3");
    });
    
    dispatch_barrier_async(queue, ^{
        NSLog(@"dispatch_barrier_async");
        [NSThread sleepForTimeInterval:5];
    });
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@"dispatch_async4");
    });
}


-(void)methods4{
    dispatch_queue_t globalQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_apply(3, globalQ, ^(size_t index) {
         NSLog(@"dispatch_apply");
    });

}

//5、dispatch_after
-(void)methods5{
    dispatch_queue_t queue = dispatch_queue_create("me.tutuge.test.gcd", DISPATCH_QUEUE_SERIAL);
    //立即打印一条信息
    
    NSLog(@"Begin add block...");
    //提交一个block
    dispatch_async(queue, ^{
        //Sleep 10秒
        [NSThread sleepForTimeInterval:10];
        
        NSLog(@"First block done...");
    });
    //5 秒以后提交block
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), queue, ^{
        
        NSLog(@"After...");
    });
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
